﻿using System;
using System.Collections.Generic;
using MvpFramework;
using MvpFramework.DesignByContract;
using NUnit.Framework;
using Rhino.Mocks;

namespace MvpFramework.UnitTests
{
    [TestFixture]
    public class A_ValidationManager
    {
        #region Setup/Teardown

        [SetUp]
        public void Arrange()
        {
            _mocker = new MockRepository();
            _validationManager = _mocker.PartialMock<ValidationManager>();
            validationMessage = _mocker.Stub<IValidationMessage>();
        }

        [TearDown]
        public void AssertAll()
        {
            _mocker.VerifyAll();
        }

        #endregion

        private MockRepository _mocker;
        private ValidationManager _validationManager;
        private IValidationMessage validationMessage;

        public void Act()
        {
            SetupResults();
            _mocker.ReplayAll();
        }

        public void SetupResults()
        {
            _mocker.Replay(validationMessage);
            var allLabels = new List<IValidationMessage> {validationMessage};
            SetupResult.For(_validationManager.GetAllValidationLabels()).Return(allLabels);
        }

        [Test]
        [ExpectedException(typeof (PreconditionException))]
        public void can_not_have_a_null_find_label_function()
        {
            Expect.Call(_validationManager.FindLabelFunction).Return(null);
            Act();
            _validationManager.GetValidationLabelFor(null);
        }

        [Test]
        public void GetValidationLabelFor_executes_FindLabelFunction()
        {
            var getLabelFunc = new Func<object, IValidationMessage>(o => validationMessage);
            Expect.Call(_validationManager.FindLabelFunction).Return(getLabelFunc);
            LastCall.Repeat.AtLeastOnce();
            Act();
            _validationManager.GetValidationLabelFor(null);
        }
        [Test]
        public void initially_sets_all_ILabels_visibility_to_false()
        {
            validationMessage.Visible = false;
            Act();
        }
    }
}